var cheerio = require('cheerio');
var request = require('request');
var driver = require('../dao/db');


var async = require('async');
var pinyin = require("node-pinyin");

const table = 'manhua';
const Url = "mongodb://localhost:27017/manhua";

var lastId ='5a4f358da5ec260f0847210c';

var getOne = function () {
    var _query = {};
    if (lastId) {
        _query = { '_id': { '$gt': require('mongodb').ObjectId(lastId) } };
    }

    // var _query = {'_id': { '$gt': require('mongodb').ObjectId(lastId) }};
    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 查找
        function (db, callback) {
            driver.pageById(db, callback, table, _query, 1);
            // driver.page(db, callback, table, {}, _default.p, 1);
        },

        function (db, res, callback) {
            driver.close(db, res, callback);
        }
    ],
        // result
        function (err, result) {
            console.log("getOne");
            // console.log(result);
            if (err) {
                console.log(err);
                return;
            }

            console.log(result.data);

            if (result.data.length > 0) {

                var item = result.data[0]
                lastId = item['_id'];

                var title = item['title'];

                var letter = sort(title);

                console.log("------------letter:" + letter);
                update(lastId, { letter: letter });
            } else {
                console.log('update all');
            }
        }
    );
};



function sort(title) {
    title = title.replace(/(^\s*)|(\s*$)/g, "");
    let str = title.substring(0, 1).toUpperCase();

    // 英文字符，数字
    if (str >= 'A' && str <= 'Z') {
        return str;
    } else if (str >= 0 && str <= 9) {
        return '0-9';
    } else {

        var p = pinyin(str, { "style": "normal" })[0][0].toUpperCase();
        console.log(p);
        p = p.substring(0, 1).toUpperCase();


        console.log((p >= 'A') && (p <= 'Z'));

        if ((p >= 'A') && (p <= 'Z')) {
            return p;
        } else {
            return '其他';
        }


    }


}

var update = function (_lastId, _update) {


    var _updateQuery = { $set: _update };

    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 分页
        function (db, callback) {
            driver.update(db, callback, table, { "_id": _lastId }, _update);
        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }
    ],
        // result
        function (err, result) {

            if (err) {
                console.log(err);
                return;
            }
            console.log("-----update success-----");
            if (result.success) {

                // getOne();


            } else {
                console.log('error');
                console.log(result);
            }

        }
    );
};

getOne();




